package org.fao.geonet.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ExcelExportServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		OutputStream out = response.getOutputStream();
		try {
			if (request.getParameter("excel_html_content") != null
					&& request.getParameter("file_name") != null) {

				String content = request.getParameter("excel_html_content");
				String fileName = request.getParameter("file_name");
				response.addHeader("Content-Type", "application/vnd.ms-excel;");
				response.addHeader("Content-Disposition",
						"attachment; filename="
								+ fileName.replaceAll("[ ]", "_") + ";");
				out.write(content.getBytes());
				out.flush();
				out = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}
}
